package com.cloudptt.api.product.func.call;

import com.cloudptt.api.pjsip.api.SipManager;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.call.group.GroupCallRecvListener;
import com.cloudptt.api.product.func.call.group.GroupCallSpeakListener;
import com.cloudptt.api.product.func.call.group.GroupCallTransListener;
import com.cloudptt.api.product.func.call.group.fsm.GroupCallConferenceListener;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.db.DBAdapter;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;

import android.content.Context;
import android.content.Intent;

public class CallFunc implements IFunc,ICallHistory
{
	private final static String Log_Key = "CallFunc";

//	private GroupCallBroadcast broadcast = null;
	private Context ctx = null;
	private CallTracker callTracker = null;
	private DBAdapter dbOperater = null;

	public CallFunc(Context ctx, CallTracker callTracker, DBAdapter dbOperater)
	{
		Log.d(CallFunc.Log_Key, "CallFunc");
		this.ctx = ctx;
		this.callTracker = callTracker;
		this.dbOperater = dbOperater;
		this.callTracker.setCallHistory(this);
	}
	public void setPCallClass(Class<?> activityCls,Class<?> floatWindowServiceCls)
	{
		this.callTracker.setPCallClass(activityCls,floatWindowServiceCls);
	}
	public void setIncommingCall(IIncommingCall incommingCall)
	{
		this.callTracker.setIncommingCall(incommingCall);
	}
	public void setGCallVideoClass(Class cls)
	{
		this.callTracker.setGCallVideoClass(cls);
	}
	public void setSingleCallListener(CallListener callListener)
	{
		Log.d(CallFunc.Log_Key, "CallFunc setSingleCallListener");
		callTracker.setSingleCallListener(callListener);
	}
	public void setShowFloatWin(String callKey, boolean isLaucher)
	{
		Log.d(CallFunc.Log_Key, "CallFunc setSingleCallLaucher");
		callTracker.setShowFloatWin(callKey,isLaucher);
	}
	
	public void setGCallListener(CallListener callListener,GroupCallSpeakListener floorListener)
	{
		Log.d(CallFunc.Log_Key, "CallFunc setGCallListener");
		this.callTracker.setGCallListener(callListener,floorListener);
	}
	public void setGCallVideoListener(CallListener callListener, GroupCallTransListener transListener, GroupCallRecvListener recvListener)
	{
		Log.d(CallFunc.Log_Key, "CallFunc setGCallListener");
		this.callTracker.setGCallVideoListener(callListener,transListener,recvListener);
	}

	public void setGCallConferenceListener(GroupCallConferenceListener listener)
	{
		Log.d(CallFunc.Log_Key, "CallFunc setGCallConferenceListener");
		this.callTracker.setGCallConferenceListener(listener);
	}
//	public void setSurfaceView(int callId,SurfaceViewRenderer localRender,SurfaceViewRenderer remoteRender)
//	{
//		callTracker.setSurfaceView(callId, localRender, remoteRender);
//	}
//	public int getTalkState()
//	{
//		return this.callTracker.getState();
//	}
	
//	public CallInfo getTalkInfo(int callId)
//	{
//		return this.callTracker.getTalkInfo(callId);
//	}

	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc)
	{
		if(callTracker != null)
		{
			callTracker.onLogined(mcIdInfo,info,isLoginSuc);
		}
//		this.groups.groupsChanged();//防止切换用户时，当前用户显示的是上一个用户的组，后期考虑优化
	}
	@Override
	public void onPublishResult(int publishResult)
	{

	}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(CallFunc.Log_Key, "doNetworkChange isConn = " + isConn);
		this.callTracker.doNetworkChange(isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.d(CallFunc.Log_Key, "doForceDetach");
		this.callTracker.doForceDetach();
	}
	@Override
	public void doLogout()
	{
		Log.d(CallFunc.Log_Key, "CallFunc logout");
		this.callTracker.doLogout();
	}
	@Override
	public void doExit()
	{
		Log.d(CallFunc.Log_Key, "CallFunc doExit");
		this.callTracker.doExit();
		callTracker = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		callTracker.onKeepAlive(isconnected);
	}
	@Override
	public void newCallHistory(AddrInfo receiver,AddrInfo sender,int direction,boolean isAdd,String contentStr) {
		// TODO Auto-generated method stub
		Log.d(CallFunc.Log_Key, "newCallHistory receiver = " + receiver + " sender = " + sender + " isAdd = " + isAdd + " contentStr = " + contentStr);
		int msgType = SessionInfo.ContentType_Text;

		SessionInfo sessionInfo = new SessionInfo(receiver,sender,msgType,direction,contentStr,0);
		dbOperater.insertNewSession(sessionInfo, isAdd);
		
		
		Intent intent = new Intent(SipManager.ACTION_SIP_CALL_LOG);
		ctx.sendBroadcast(intent,
				SipManager.PERMISSION_USE_SIP);
	}
}
